<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field['options'].displayStyle"
  >
    <div class="scene-public-board" :style="getStyle('area')">
      <div class="board-wrap">
        <div class="board-inside flex-box">
          <template v-for="(patientItem,index) in getPatientListInfo('list')">
            <div class="flex-item" :key="index">
              <!--头部-->
              <div class="header-area" :style="getStyle('header')">
                <template v-for="(item) in (optionsInfo['colOptions'])">
                  <div class="header-item item" :class="item.className"
                       :style="setFlex(item,'head')"
                       v-if="optionsInfo['defaultShowCol'].includes(item['value'])"
                  >
                    {{ item.label }}
                  </div>
                </template>
              </div>
              <!--列表区域-->
              <div class="list-area" ref="listRef">
                <div class="list-info"
                     v-for="(item,index) in patientItem" :key="index"
                     :style="getStyle('listItem')"
                     :data-info="item">
                  <template v-if="true" v-for="(items) in optionsInfo['colOptions']">
                    <div class="list-item" :class="items.className"
                         :style="setFlex(items,'listItem')"
                         v-if="optionsInfo['defaultShowCol'].includes(items['value'])"
                    >
                      <template v-if="items.value==='totalSource'">
                        <div class="patient-name" v-if="item[items['value']]">
                          <span>{{ item[items['value']] || '\u00a0' }}</span>
                        </div>
                      </template>
                      <template v-else-if="items.value==='deptName'">
                        <template v-if="item[items['value']]">
                          <ScrollX :key="getOnlyId()">
                            {{ item[items['value']] || '\u00a0' }}
                          </ScrollX>
                        </template>
                      </template>
                      <template v-else>
                        {{ item[items['value']] || '\u00a0' }}
                      </template>
                    </div>
                  </template>
                </div>
              </div>
            </div>
          </template>
        </div>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import {v4 as uuid} from "uuid";
import {pxToVw} from "@/utils/common";
import emitter from "@/utils/designer/emitter";
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import ScrollX from "@c/public/seamlessScroll/ScrollX.vue";
import SvgIcon from "@c/public/svg-icon/index.vue";
import {bizFields, containers, customFields, sceneFields} from "@c/public/form-designer/widgetsConfig";

export default {
  name: "scene-public-numberSource-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    SvgIcon,
    ScrollX,
    CompOperate,
  },

  props: {
    widget: Object,
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    devType: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */
      type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */
      type: String,
      default: "",
    },
    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
  },

  data() {
    return {
      optionsInfo: Array(),
    }
  },


  computed: {
    //生成唯一的字符串
    getOnlyId() {
      return function () {
        return uuid()
      }
    },

    // 模板的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Object();
        let Obj = Object();
        let {patientListTotal} = this.optionsInfo;
        switch (val) {
          case "area":
            Reflect.set(Obj, "height", pxToVw(info.areaHeight));
            return Obj;
          case "header":
            let header = (info.header);
            Reflect.set(Obj, "color", (header.color));
            Reflect.set(Obj, "fontSize", pxToVw(header.fontSize));
            Reflect.set(Obj, "fontWeight", (header.fontWeight));
            Reflect.set(Obj, "background", (header.background));
            return Obj;
          case "list":
            let list = (info.list);
            Reflect.set(Obj, "color", (list.color));
            Reflect.set(Obj, "fontSize", pxToVw(list.fontSize));
            Reflect.set(Obj, "fontWeight", (list.fontWeight));
            return Obj;
          case "listItem":
            let element = (this.$refs.listRef);
            if (element?.length) {
              const {height} = element[0]?.getBoundingClientRect();
              Reflect.set(Obj, "height", pxToVw(height / patientListTotal));
            }
            return Obj;
        }
      };
    },

    // 设置宽度和字号
    setFlex() {
      return function (item, val) {
        const self = this;
        let info = (self.optionsInfo) || Object();
        let Obj = Object();
        switch (val) {
          case "head":
            Reflect.set(Obj, "flex", (item.flex) || 1);
            return Obj;
          case "listItem":
            let list = (info.list);
            Reflect.set(Obj, "color", (list.color));
            Reflect.set(Obj, "fontSize", pxToVw(list.fontSize));
            Reflect.set(Obj, "fontWeight", (list.fontWeight));
            Reflect.set(Obj, "flex", (item.flex) || 1);
            return Obj;
        }
      }
    },

    //获取患者信息--截取总量
    getPatientListInfo() {
      return function (type) {
        let list;
        let devType = (this.devType);
        let boardPatientInfo = this.$store.getters['getPublishNumberSourceListInfo'];
        let {patientListTotal} = this.optionsInfo;
        if (devType) {
          list = boardPatientInfo['apiData']
        } else {
          list = boardPatientInfo['mockData']
        }
        switch (type) {
          case 'list':
            if (!!list && !!(list.length)) {
              let flattenArr = list.slice(0, patientListTotal);
              if (!!flattenArr && (flattenArr.length) >= patientListTotal) {
                return this.$lodash.chunk(flattenArr, patientListTotal)
              } else {
                let arr = (new Array(patientListTotal - flattenArr.length).fill({}));
                let flattenArr2 = [...flattenArr, ...arr];
                return this.$lodash.chunk(flattenArr2, patientListTotal)
              }
            } else {
              let arr = (new Array(patientListTotal).fill({}));
              return this.$lodash.chunk(arr, patientListTotal)
            }
        }
      }
    },
  },

  mounted() {
    this.handleOnMounted();
  },

  methods: {
    // 接收父组件传递过来的数据
    async executeMethod(key, data) {
      switch (key) {
        case "publishListInfo":
          let publicListInfo = this.$lodash.cloneDeep(data);
          await this.$store.dispatch('setPublishNumberSourceListInfo', publicListInfo)
          break
      }
    }
  },

  watch: {
    // 获取配置项
    field: {
      handler: function (val) {
        let key = (val.options.config);
        this.optionsInfo = (val.options[key]);
      },
      deep: true,
      immediate: true
    }
  },

  destroyed() {
    this.unregisterFromRefList();
  },

};
</script>

<style lang="scss" scoped>
.scene-public-board {
  background-color: transparent;
  color: #333;
  height: 100%;
  line-height: 100%;
  padding: 10px 18px;
  box-sizing: border-box;

  .board-wrap {
    height: calc(100%);

    .board-inside {
      height: 100%;

      .flex-item {
        overflow: hidden;
        border-radius: 16px;
        border: none;
        display: flex;
        width: 100%;
        flex-direction: column;

        .header-area {
          display: flex;
          color: #EBEEF5;
          padding: 14px 0;
          width: 100%;
          height: 100px;
          align-items: center;
          align-self: center;
          overflow: hidden;
          box-sizing: border-box;
          background: #0059B3;

          div.header-item {
            flex: 1;
            white-space: nowrap;
            //line-height: 36px;
            text-align: center;
            height: 100%;
            display: flex;
            align-self: center;
            align-items: center;
            align-content: center;
            justify-content: center;
          }
        }

        .list-area {
          background: white;
          display: flex;
          flex-direction: column;
          height: calc(100% - 100px);
          width: 100%;
          box-sizing: border-box;
          overflow: hidden;

          .list-info {
            display: flex;
            flex-direction: row;
            flex-grow: 1;
            overflow: hidden;
            width: 100%;
            align-self: center;
            align-items: center;
            justify-items: center;
            border-top: 6px #DCDFE6 solid;
            height: 110px;

            &:first-child {
              border-top: none;
            }

            .list-item {
              font-size: 32px;
              overflow: hidden;
              text-align: center;
              color: #333333;
              height: 100%;
              white-space: nowrap;
              position: relative;
              min-width: 30px;
              border-right: 10px #DCDFE6 solid;
              background: rgba(173, 255, 47, 0.01);
              @include singleEllipsis(100%);
              display: inline-flex;
              justify-content: center;
              align-self: center;
              align-items: center;
              justify-items: center;
              flex: 1;
              flex-wrap: nowrap;


              &:last-child {
                border-right: none;
              }

              .patient-name {
                display: inline-flex;
                align-items: center;
                background: rgba(0, 89, 179, 0.2);
                border-radius: 8px;
                overflow: hidden;
                margin: 0 auto;
                justify-items: center;
                height: calc(100% - 8px - 8px);
                flex-basis: calc(100% - 8px - 8px);
                box-sizing: border-box;

                span {
                  display: flex;
                  height: 100%;
                  flex-basis: 0;
                  margin: 0 auto;
                  align-items: center;
                }
              }

              .sign {
                width: 34px;
                height: 34px;
                border-radius: 4px;
                overflow: hidden;
                margin-left: 16px;
              }

              .sign-1 {
                background: #37BC9C;
              }

              .sign-2 {
                background: #F64848;
              }

              .sign-3 {
                background: #0059B3;
              }

              .sign-4 {
                background: #E58602;
              }
            }

            .state {
              justify-content: left;
              width: 100%;

              .describe {
                padding-left: 36px;
              }
            }

          }
        }
      }
    }
  }
}

</style>